<template>
  <div>
    <Form ref="form"  :model="form">
      <FormItem>
        <Input placeholder="输入关键字进行过滤" v-model="filterText" />
      </FormItem>
    </Form>
    <Tree  
    :data="data" 
    select-node
    :load-data="loadData"
    :check-strictly="true"
    :check-directly="true"
    @on-select-change="treeChange">
    </Tree>
    <Row class="code-row-bg" justify="end" type="flex">
      <Button @click="cancel" style="margin-right: 10px">取消</Button>
      <Button @click="save" type="primary">保存</Button>
    </Row>
  </div>
</template>
<script>
import { fileApi } from "@/api/file.js";
export default {
  name: "CodeReviewListForm",
  components: {},
  props: {
    //是否为更新表单
    moveList: {
      type: Boolean,
      default: true,
    },
    
  },
  data() {
    return {
      filterText: null,
      flag: 0,
      data: [
        {
          title: "一级 1",
          loading: false,
          children: []
        }
      ],
      selectNode:null,
      sourceFileList:[]
    };
  },
  watch: {
    moveList: function (newValue, oldValue) {
        if(newValue.length){
            let list = []
            newValue.forEach(item=>{
                let obj = {
                    fileMode: item.fileMode,
                    fileName: item.fileName,
                    id: item.id,
                    parentFolderId: item.parentFolderId,
                }
                list.push(obj);
            })
            this.sourceFileList = list;
        }
          
    },
    
    filterText(val) {
      let arr = JSON.parse(JSON.stringify(this.data2));
      this.data = this.filterNode(val, arr);
    },
  },
  mounted() {
      //初始化获取一级文件夹
      this.getRootFolder();
  },
  created() {},
  methods: {
    cancel() {
      this.$emit("on-form-close");
    },
    // 获取邮件数据
    async getRootFolder() {
      let list = [];
       let searchForm = {
        fileClass: null,      //标识文件文档 还是视频  音频  压缩包之类 
        fileMode: 1,       //文件是普通文件 0  还是文件夹  1
        fileName: null,       //文件名称关键字
        fileType: null,  //文件类型 就是JPEG MP3 MP4 AVI 等等
        pageNum: 1,
        pageSize: 1000,
        parentFolderId: 0,  //初始化文件夹父节点为0
        searchCount: true, // 是否查询总条数
      }
      let res = await fileApi.queryFileManageList(searchForm);
      if(res.data.list.length){
          res.data.list.forEach(item=>{
              item.title = item.fileName;
              item.children = [];
              item.loading = false;
              list.push(item);
          })
      }
      this.data = list;
    },
    
    async save() {
        if(!this.selectNode){
            this.$Message.warning("请选择文件夹");
        }
        let param = {
            destFolderId: this.selectNode.id,
            sourceFileList: this.sourceFileList
        }
        this.$Spin.show();
        fileApi.moveFile(param).then(res=>{
            this.$Message.success(res.msg);
            this.$Spin.hide();
            this.resetForm();
            this.$emit("on-form-close"); 
        });
    },
    loadData (item, callback) {
        let list = [];
          let searchForm = {
          fileClass: null,      //标识文件文档 还是视频  音频  压缩包之类 
          fileMode: 1,       //文件是普通文件 0  还是文件夹  1
          fileName: null,       //文件名称关键字
          fileType: null,  //文件类型 就是JPEG MP3 MP4 AVI 等等
          pageNum: 1,
          pageSize: 1000,
          parentFolderId: item.id,  //初始化文件夹父节点为0
          searchCount: true, // 是否查询总条数
        }
        fileApi.queryFileManageList(searchForm).then(res=>{
            if(res.data.list.length){
                res.data.list.forEach(item=>{
                    item.title = item.fileName;
                    item.children = [];
                    item.loading = false;
                    list.push(item);
                })
            }
            callback(list);
        });
        
    },
    resetForm() {
      this.form = {
        //品种
        kind: null,
        //名字
        name: null,
        //颜色
        color: null,
        //图片链接
        imageUrl: null,
      };
      this.$refs["form"].resetFields();
    },
    //过滤树节点
    filterNode(val, List) {
      //过滤出满足条件的数组
      let List1 = List.filter((item) => {
        //如果该元素有children，则优先处理children的
        if (item.children) {
          //过滤children里面满足条件的
          item.children = this.filterNode(val, item.children);
          //如果children里面没有满足条件的，则进入，否则返回true
          if (!item.children.some((item1) => item1.title.indexOf(val) !== -1)) {
            //判断children里面是否还有一层children,如果有则对内层children继续调用函数判断，否则返回title中含有关键字的元素
            if (item.children.some((item1) => item1.children)) {
              item.children.forEach((item2) => {
                item2.children && this.filterNode(val, item2.children);
              });
            } else {
              return item.title.indexOf(val) > -1;
            }
          }
          return true;
        }
        //返回title中含有关键字的元素
        return item.title.indexOf(val) > -1;
      });
      return List1;
    },
    treeChange(arr, obj) {
       this.selectNode = arr[0]
       console.log(arr, obj, 1234567);
     }
  },
};
</script>